import spaceObject
import trinity
import string

class Sun(spaceObject.SpaceObject):
    __guid__ = 'spaceObject.Sun'

    def __init__(self):
        spaceObject.SpaceObject.__init__(self)
        self.useOccluders = False
        self.useGodRays = False
        self.forceLOD = True
        self.sunEffectsEnabled = settings.user.ui.Get('effectsEnabled', 1)



    def LoadModel(self):
        slimItem = sm.StartService('michelle').GetBallpark().GetInvItem(self.id)
        graphicURL = cfg.invtypes.Get(slimItem.typeID).GraphicFile()
        s2GraphicURL = graphicURL.replace(':/Model/', ':/dx9/Model/').replace('.blue', '.red')
        if self.useGodRays:
            s2GraphicURL = s2GraphicURL.replace('.red', '_godrays.red')
        self.lensflare = trinity.Load(s2GraphicURL)
        self.EnableOccluders(settings.public.device.Get('sunOcclusion', 1))
        scene2 = sm.GetService('sceneManager').GetRegisteredScene2('default')
        scene2.sunBall = self
        baseName = string.split(graphicURL, '/')
        baseName = baseName[-1]
        sunModelURL = ('res:/dx9/Model/WorldObject/Sun/' + baseName.replace('.blue', '.red'))
        self.sunmodel = trinity.Load(sunModelURL)
        self.model = trinity.EvePlanet()
        self.model.translationCurve = self
        self.model.rotationCurve = self
        self.model.name = ('%d' % self.id)
        self.model.ready = True
        self.sunmodel.name = self.model.name
        self.model.highDetail = self.sunmodel
        self.model.resourceCallback = self.ResourceCallback
        scene2.planets.append(self.model)
        self.lensflare.translationCurve = self
        scene2.lensflares.append(self.lensflare)



    def EnableOccluders(self, enable):
        if (self.lensflare is None):
            return 
        self.lensflare.doOcclusionQueries = enable



    def EnableGodRays(self, enable):
        if (self.useGodRays == enable):
            return 
        if (self.lensflare is None):
            return 
        if not self.released:
            self.ReleaseSun()
        self.useGodRays = enable
        self.LoadModel()



    def SetGodRaysIntensity(self, intensity):
        if not self.useGodRays:
            return 
        if (self.lensflare is None):
            return 
        for flare in self.lensflare.flares:
            if (flare.mesh is not None):
                for area in flare.mesh.additiveAreas:
                    if (area.effect is not None):
                        if area.effect.effectFilePath.endswith('GR.fx'):
                            for param in area.effect.parameters:
                                if (param.name == 'intensity'):
                                    param.value = (param.value[0],
                                     param.value[1],
                                     param.value[2],
                                     intensity)






    def ResourceCallback(self, create, size = 2048):
        self.model.ready = True
        self.model.resourceActionPending = False



    def Assemble(self):
        self.model.scaling = self.radius
        self.model.radius = (self.radius / 10.0)



    def Release(self):
        if self.released:
            return 
        self.ReleaseSun()
        spaceObject.SpaceObject.Release(self, 'Sun')



    def ReleaseSun(self):
        scene2 = sm.GetService('sceneManager').GetRegisteredScene2('default')
        if hasattr(self.model, 'resourceCallback'):
            self.model.resourceCallback = None
        if hasattr(self.model, 'children'):
            del self.model.children[:]
        scene2.planets.fremove(self.model)
        self.model.translationCurve = None
        self.model.rotationCurve = None
        self.model = None
        self.sunmodel = None
        self.lensflare = None
        scene2.sunBall = None
        del scene2.lensflares[:]



exports = {'spaceObject.Sun': Sun}

